module.exports = {
	/**
	 * 查询角色列表
	 * @url client/wechat/selects/pub/selects 前端调用的url参数地址
	 * 表名
	 * @dbName 查询的表的名称
	 * data 请求参数 说明
	 * @param {String}         _id  用户编号
	 * res 返回参数说明
	 * @param {Number}         code      错误码，0表示成功
	 * @param {String}         msg       详细信息
	 */
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '' };
		// 业务逻辑开始-----------------------------------------------------------
		let {tip_id, user_id} = data
		
		// 创建回滚
		const transaction = await vk.baseDao.startTransaction();
		try {
			// 查询当前封面
			let tip = await vk.baseDao.findById({
				dbName: "bus-wx-tip",
				id: tip_id
			})
			if(!tip) return { code: -1, msg:"封面不存在！" }
			
			// 查询用户信息
			let user = await vk.baseDao.findById({
				dbName: "uni-id-users",
				id: user_id
			})
			if(!user) return { code: -1, msg:"用户不存在！" }
			if(user.status != 0) return { code: -1, msg:"用户已被封禁！" }
			if(user.score < tip.score) return { code: -1, msg:"余额不足！" }
			
			// 查询封面CDK
			// let cdkCount = await vk.baseDao.count({
			// 	dbName: "bus-wx-tip-cdk",
			// 	whereJson: {
			// 		tip_id: _id,
			// 		status: 1
			// 	}
			// })
			// if(cdkCount === 0) return { code: -1, msg:"库存不足！" }
			let cdk = await vk.baseDao.findByWhereJson({
				dbName:"bus-wx-tip-cdk",
				whereJson:{
					tip_id,
					status: 1
				}
			})
			if(!cdk) return { code: -1, msg:"库存不足！" }
			
			// 更新cdk状态
			let cdks = await vk.baseDao.updateById({
				db:transaction,
				dbName: "bus-wx-tip-cdk",
				id: cdk._id,
				dataJson: {
					status: 0
				},
				getUpdateData:true
			});
			if(!cdks){
				await transaction.rollback();
				return { code: -1, msg:"CDK更新失败！" }
			}
			
			// 执行数据库API请求
			let users = await vk.baseDao.updateById({
				db:transaction,
				dbName: "uni-id-users",
				id: user_id,
				dataJson: {
					score: _.inc(-tip.score)
				},
				getUpdateData:true
			});
			if(!users){
				await transaction.rollback();
				return { code: -1, msg:"积分更新失败！" }
			}
			
			var scoreId = await vk.baseDao.add({
				db:transaction,
				dbName: "uni-id-scores",
				dataJson: {
					user_id,
					type: 2,
					score: tip.score,
					balance: user.score,
					comment: "兑换封面："+tip.name
				}
			})
			if(!scoreId){
				await transaction.rollback();
				return { code: -1, msg:"积分记录更新失败！" }
			}
			
			var orderId = await vk.baseDao.add({
				db:transaction,
				dbName: "bus-wx-tip-order",
				dataJson: {
					tip_id,
					user_id,
					score: tip.score,
					number: vk.pubfn.createOrderNo("HF",16),
					cdk: cdk.cdk
				}
			})
			if(!scoreId){
				await transaction.rollback();
				return { code: -1, msg:"积分记录更新失败！" }
			}
			
			// 提交事物
			await transaction.commit();
			res.score = users.score
			res.cdk = cdk.cdk
			res.msg = "兑换成功！"
		}catch (err) {
			// 事务回滚
			await transaction.rollback();
			return {
				code: -1,
				msg:"数据库写入异常,事务已回滚",
				err: {
					message:err.message,
					stack:err.stack
				}
			}
		}
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}

}